package cn.bm.web.db;

import com.zaxxer.hikari.HikariConfig;
import com.zaxxer.hikari.HikariDataSource;

import java.io.InputStream;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.Properties;

/**
 * Hikari连接池
 */
public class DBConfig {
    private static HikariDataSource dataSource;
    public static Properties prop;
    static {
        try {
            prop = new Properties();

            InputStream resource;
            resource = DBConfig.class.getClassLoader().
                    getResourceAsStream("db.properties");
            prop.load(resource);

            //初始化HikariConfig配置 druid
            HikariConfig config = new HikariConfig();//连接池
            config.setJdbcUrl(prop.getProperty("jdbcUrl"));
            config.setUsername(prop.getProperty("username"));
            config.setPassword(prop.getProperty("password"));
            config.addDataSourceProperty("cachePrepStmts", prop.getProperty("dataSource.cachePrepStmts"));
            config.addDataSourceProperty("prepStmtCacheSize", prop.getProperty("dataSource.prepStmtCacheSize"));
            config.addDataSourceProperty("prepStmtCacheSqlLimit", prop.getProperty("dataSource.prepStmtCacheSqlLimit"));
            config.addDataSourceProperty("useServerPrepStmts", prop.getProperty("dataSource.useServerPrepStmts"));
            config.addDataSourceProperty("useLocalSessionState", prop.getProperty("dataSource.useLocalSessionState"));
            config.addDataSourceProperty("rewriteBatchedStatements", prop.getProperty("dataSource.rewriteBatchedStatements"));
            config.addDataSourceProperty("cacheResultSetMetadata", prop.getProperty("dataSource.cacheResultSetMetadata"));
            config.addDataSourceProperty("cacheServerConfiguration", prop.getProperty("dataSource.cacheServerConfiguration"));
            config.addDataSourceProperty("elideSetAutoCommits", prop.getProperty("dataSource.elideSetAutoCommits"));
            config.addDataSourceProperty("maintainTimeStats", prop.getProperty("dataSource.maintainTimeStats"));
            //初始化HikariDataSource
            dataSource = new HikariDataSource(config);

        } catch (Exception e) {
            System.exit(1);
            throw new RuntimeException("HikariCP 初始化失败", e);
        }

    }

    /**
     * 获取数据库连接
     * @return
     */
    public static Connection getConnection(){
        try {
            return dataSource.getConnection();
        } catch (SQLException e) {
            e.printStackTrace();
            System.exit(1);
            throw new RuntimeException("数据库连接失败", e);
        }
    }
}